package User

import (
	"FromNowOnPromote/博客系统/Tblog/controllers"
	"FromNowOnPromote/博客系统/Tblog/jwt"
	"FromNowOnPromote/博客系统/Tblog/models"
	"FromNowOnPromote/博客系统/Tblog/models/User"
	"FromNowOnPromote/博客系统/Tblog/redis"
	"encoding/json"
	"github.com/mojocn/base64Captcha"
	"strconv"
)

//登录控制器
type LoginController struct {
	controllers.MainController
}

//ConfigJsonBody json request body.
type ConfigJsonBody struct {
	//用户登录信息
	AccountName string
	Password    string

	//验证码信息
	Id          string
	VerifyValue string
}

type UserLockedCheck struct {
	User_locked int
}

//处理登录表单
func (this *LoginController) LoginPost() {
	//解析请求参数
	r := this.Ctx.Request
	decoder := json.NewDecoder(r.Body)
	var postParameters ConfigJsonBody
	err1 := decoder.Decode(&postParameters)
	if err1 != nil {
		body := map[string]interface{}{"code": 300, "data": "验证失败", "msg": "提交表单信息不全"}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//校验提交信息完整性
	if postParameters.VerifyValue == "" || postParameters.Password == "" || postParameters.AccountName == "" || postParameters.Id == "" {
		body := map[string]interface{}{"code": 301, "data": "验证失败", "msg": "提交表单信息异常"}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}
	//verify the captcha
	//比较图像验证码
	verifyResult := base64Captcha.VerifyCaptcha(postParameters.Id, postParameters.VerifyValue)

	//检测用户是否被封禁
	db := models.ConnDb()
	defer db.Close()
	var ulc UserLockedCheck
	err6 := db.Model(&User.TbUser{}).Where("username = ?", postParameters.AccountName).Scan(&ulc).Error
	if err6 != nil || ulc.User_locked == 0 {
		body := map[string]interface{}{"code": 306, "data": "验证失败", "msg": "用户已被封禁"}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}

	//set json response
	if verifyResult {
		//登录查表验证
		user := User.TbUser{}
		//查询用户名是否存在
		err3 := db.Where("username = ?", postParameters.AccountName).Find(&user).Error //查找数据库
		if err3 != nil {
			body := map[string]interface{}{"code": 303, "data": "验证失败", "msg": "用户名不存在"}
			this.Data["json"] = body
			this.ServeJSON()
			return
		} else {
			if user.Password == postParameters.Password {
				body := map[string]interface{}{"code": 200, "data": "验证通过", "msg": "登录成功", "userInfo": map[string]interface{}{
					"username": user.Username, "authorImg": user.IconUrl}}
				token := jwt.GenToken() //生成一个token

				this.Ctx.SetCookie("uuid", token, 1000000, "/")
				rd := redis.R.Conn
				defer rd.Close()
				_, err4 := rd.Set(token, strconv.Itoa(user.Uuid))
				if err4 != nil {
					body = map[string]interface{}{"code": 305, "data": "验证失败", "msg": "服务器异常"}
					this.Data["json"] = body
					this.ServeJSON()
					return
				}
				_, err5 := rd.Expire(token, 1000000) //token生存周期
				if err5 != nil {
					body = map[string]interface{}{"code": 305, "data": "验证失败", "msg": "服务器异常"}
					this.Data["json"] = body
					this.ServeJSON()
					return
				}
				//this.Ctx.SetCookie("token",token)
				this.Data["json"] = body
				this.ServeJSON()
			} else {
				body := map[string]interface{}{"code": 304, "data": "验证失败", "msg": "密码错误"}
				this.Data["json"] = body
				this.ServeJSON()
				return
			}
		}
	} else {
		body := map[string]interface{}{"code": 302, "data": "验证失败", "msg": "验证码无效"}
		this.Data["json"] = body
		this.ServeJSON()
		return
	}

}
